package SiTuQingYou.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

import SiTuQingYou.dao.LikeCollectDao;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.LikeCollect;
import SiTuQingYou.vo.User;

/**
 * Servlet implementation class MyCollectionServlet
 */
@WebServlet("/MyCollectionServlet")
public class MyCollectionServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyCollectionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    //根据多个攻略id查询多个总赞数
    protected void getManyToatalLike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String[] cid= request.getParameterValues("cid");
    	String totals="";
    	if(cid==null)
    	{
    		
    	}
    	else
    	{
    		for(String id:cid)
        	{
        	   int total=LikeCollectDao.getlcd().getlike(Integer.parseInt(id));
        	   totals=totals+","+total;
        	}
        	PrintWriter out=response.getWriter();
        	out.print(totals);
        	
    	}
    	
    	 
    }
    
    //根据一个攻略id查询总赞数
    protected void getToatalLike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String id= request.getParameter("id");
    	int cid=Integer.parseInt(id);
    	int total=LikeCollectDao.getlcd().getlike(cid);
    	PrintWriter out=response.getWriter();
    	out.print(total);
    }

    //根据用户id查询到他所收藏的攻略
    protected void lookForAllCollection(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stubr
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("user");//得到登录的User对象

    	Pager pager = Pager.init(request);
    	
    	ArrayList<LikeCollect> allStrategy= LikeCollectDao.getlcd().lookForAllCollectedStrategy(user.getId(),pager);
    	request.setAttribute("allStrategy",allStrategy);

	}
    
    //删除选中的攻略
    protected void deleteAllChecked(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	String cid[] = request.getParameterValues("cid");
    	LikeCollectDao.getlcd().deleteAllCheckedStrategy(cid);
    	
    	//重新查询数量，并且传给jsp页面
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("user");
    	int count = LikeCollectDao.getlcd().getCollectedStrategyNumber(user.getId());
    	PrintWriter out = response.getWriter();
 	    out.print(count);
    	
	}
    //检查攻略是否收藏，未收藏进行收藏，已收藏删除收藏弹出消息框
    protected void checkStrategyCollectionServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
    	String id= request.getParameter("id");
    	int cid=Integer.parseInt(id);
    	//System.out.println(cid);
    	HttpSession session=request.getSession();
    	User user=(User) session.getAttribute("user");
    	PrintWriter out=response.getWriter();
    	if(user==null)
    	{
    	   out.print("2");
    	}
    	else
    	{
    	   boolean flag=LikeCollectDao.getlcd().checkcollect(cid,user.getId());
    	   if(flag)
    	   {//已收藏，取消收藏
    		   
    		   LikeCollectDao.getlcd().deleteOneCollect(user.getId(), cid);
    		   out.print("1"); 
    		   
    	   }
    	   else
    	   {
    		   //收藏，先检查是否有收藏历史，有改数据，没有插入数据
    		   boolean flagx=LikeCollectDao.getlcd().checkCollectHistory(cid, user.getId());
    		   if(flagx)
    		   {
    			   LikeCollectDao.getlcd().changeOneCollect(user.getId(), cid);
    		   }
    		   else
    		   {
    			  LikeCollectDao.getlcd().addOneCollect(user.getId(), cid);
    		   }
    		   out.print("0"); 
    	   }
    	}
    	
    	
	}
    //检查攻略是否收藏，未收藏进行收藏，已收藏删除收藏弹出消息框
    protected void checkStrategyLikeServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
    	String id= request.getParameter("id");
    	int cid=Integer.parseInt(id);
    	HttpSession session=request.getSession();
    	User user=(User) session.getAttribute("user");
    	PrintWriter out=response.getWriter();
    	if(user==null)
    	{
    	   out.print("2");
    	}
    	else
    	{
    	   boolean flag=LikeCollectDao.getlcd().checkLike(cid,user.getId());
    	   if(flag)
    	   {//已点赞，取消点赞
    		   
    		   LikeCollectDao.getlcd().deleteOneLike(user.getId(), cid);
    		   out.print("1"); 
    		   
    	   }
    	   else
    	   {
    		   //点赞，先检查是否有点赞历史，有改数据，没有插入数据
    		   boolean flagx=LikeCollectDao.getlcd().checkCollectHistory(cid, user.getId());
    		   if(flagx)
    		   {
    			   LikeCollectDao.getlcd().changeOneLike(user.getId(), cid);
    		   }
    		   else
    		   {
    			  LikeCollectDao.getlcd().addOneLike(user.getId(), cid);
    		   }
    		   out.print("0"); 
    	   }
    	}
    	
    	
	}
    
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}
	

}
